package me.thinkjet.controller;

import me.thinkjet.model.Job;
import me.thinkjet.model.News;

import com.jfinal.aop.Before;
import com.jfinal.core.Controller;
import com.jfinal.ext.route.ControllerBind;
import com.jfinal.plugin.ehcache.CacheInterceptor;
import com.jfinal.plugin.ehcache.CacheName;

@ControllerBind(controllerKey = "/news", viewPath = "news")
public class NewsController extends Controller {
	@Before(CacheInterceptor.class)
	@CacheName("news")
	public void index() {
		this.setAttr("news", News.dao.paginateByCache("news",
				"index_" + this.getParaToInt(1, 1),
				this.getParaToInt(1, 1), 10, "select n.*, r.*, u.username, u.name",
				"from news n left join news_record r on n.id=r.news_id " +
						"left join users u on u.id=n.author"));
	}

	@Before(CacheInterceptor.class)
	@CacheName("news")
	public void show() {
		this.setAttr("news", News.dao.findById(this.getParaToLong()));
	}

	public void hot() {
		setAttr("hot", News.dao.findByCache("news", "news-hot",
				"select * from news where id in " +
						"(select news_id from " +
						"(select news_id from news_record order by views desc limit 0,5) as r" +
						")"));
	}
}
